<?php
namespace app\mall\controller\admin;

use suframe\thinkAdmin\traits\CURDController;
use suframe\thinkAdmin\ui\UITable;
use app\mall\model\MallRegion as MallRegionModel;

/**
* 地区设置
*/
class MallRegion extends Base
{
    use CURDController;
    protected $urlPre = '/mall/admin/mallRegion/';

    private function getManageModel()
    {
        return MallRegionModel::class;
    }

    private function ajaxSearch()
    {
        $parent_id = $this->request->param('parent_id', 0);
        $rs = $this->parseSearchWhere(
            $this->getManageModel()::where('parent_id', $parent_id),
            ['name' => 'like', 'uri' => 'like']
        )->append(['has_child']);
        return json_return($rs);
    }

    /**
     * @param \suframe\form\Form $form
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \ReflectionException
     */
    private function getFormSetting($form)
    {
        $form->setRuleByClass(\app\mall\ui\form\MallRegionForm::class);
    }

    /**
     * @param UITable $table
     */
    private function getTableSetting($table)
    {
        $table->createByClass(\app\mall\ui\table\MallRegionTable::class);
        $table->setButtons('add', ['title' => '增加', 'url' => $this->urlABuild('update')]);
        $table->setEditOps($this->urlA('update'), ['id']);
        $table->setDeleteOps($this->urlA('delete'), ['id']);

        $parent_id = $this->request->param('parent_id');
        if ($parent_id != null) {
            if ($parent_id) {
                $parent = MallRegionModel::find($parent_id);
                $parent && $table->setBreadcrumb('上一级', $this->urlABuild('index', ['parent_id' => $parent['parent_id']]));
                $table->setBreadcrumb('子菜单');
            }
        }
    }
}